﻿Imports System.Configuration
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Web
Imports System.Net
Imports Utilities.FTP.FTPclient
Imports MeoTomClient.blog.meotom.services
Imports MeoTomClient.dev.meotom.services

Module Main
    Dim lg As New Logging
    Public ProxyServer As String = ConfigurationManager.AppSettings("ProxyServer").ToString()
    Public ProxyPort As String = ConfigurationManager.AppSettings("ProxyPort").ToString()
    Public ProxyUserName As String = ConfigurationManager.AppSettings("ProxyUserName").ToString()
    Public ProxyPassword As String = ConfigurationManager.AppSettings("ProxyPassword").ToString()
    Public ProxyUserDomain As String = ConfigurationManager.AppSettings("ProxyUserDomain").ToString()
    Public FTPServer As String = ConfigurationManager.AppSettings("FTPServer").ToString()
    Public UserFTP As String = ConfigurationManager.AppSettings("UserFTP").ToString()
    Public PasswordFTP As String = ConfigurationManager.AppSettings("PasswordFTP").ToString()
    Public UploadFolder As String = ConfigurationManager.AppSettings("UploadFolder").ToString()

    Sub Main(ByVal args As String())
        If args(0) = "0" Then
            MeotomUpdate()
        ElseIf args(0) = "1" Then
            MeoTomWeatherUpdate()
        ElseIf args(0) = "2" Then
            MeoTomUpdateViewByMonth()
        ElseIf args(0) = "3" Then
            TestMail()
            'Dim iCount As Integer = 0
            'Dim str As String = "JustinSLR|justindc87@gmail.com|noF5k5x"

            'Dim strArr2 As String() = Nothing
            'Dim splitter2 As Char() = {"|"c}
            'strArr2 = str.Split(splitter2)
            'For i As Integer = 0 To strArr2.Length - 1
            '    If strArr2(i).ToString().Length > 1 Then
            '        Console.WriteLine(strArr2(i).ToString())
            '    End If
            'Next

        End If
    End Sub

    Sub TestMail()
        Try
            Dim objSrvBlog As New blog.meotom.services.MeoTomSoapClient
            objSrvBlog.SendMail("dvhai221087@gmail.com", "Test Mail", "Body Test Mail", "")
            'b = objSrvBlog.SendMail("webmaster@meotom.net", "dvhai221087@gmail.com", "Test Mail", "Test 123")
            objSrvBlog = Nothing
        Catch ex As Exception

        End Try
    End Sub


    Sub MeoTomUpdateViewByMonth()
        Try
            Dim strReturn As String = ""
            Dim objSrvBlog As New blog.meotom.services.MeoTomSoapClient
            strReturn = objSrvBlog.UpdateViewByMonth("MeoTom.Net", "m2E6o8T2o2M10")
            objSrvBlog = Nothing
            If strReturn = "False" Then
                lg.WriteLog("False dev.meotom.net")
            ElseIf strReturn = "Fail To Login" Then
                lg.WriteLog("Fail To Login Exception Dev.meotom.net")
            End If

            strReturn = ""
            Dim objSrvDev As New dev.meotom.services.MeoTomSoapClient
            strReturn = objSrvDev.UpdateViewByMonth("MeoTom.Net", "m2E6o8T2o2M10")
            objSrvDev = Nothing
            If strReturn = "False" Then
                lg.WriteLog("False blog.meotom.net")
            ElseIf strReturn = "Fail To Login" Then
                lg.WriteLog("Fail To Login Exception blog.meotom.net")
            End If
            objSrvDev = Nothing
        Catch ex As Exception
            lg.WriteLog(ex.Message)
        End Try
    End Sub

    Sub MeotomUpdate()
        Try
            Dim strReturn As String = ""
            Dim objSrvBlog As New blog.meotom.services.MeoTomSoapClient
            strReturn = objSrvBlog.UpdateHomePage("MeoTom.Net", "m2E6o8T2o2M10")
            If strReturn = "False" Then
                lg.WriteLog("False blog.meotom.net")
            ElseIf strReturn = "Fail To Login" Then
                lg.WriteLog("Fail To Login Exception blog.meotom.net")
            End If
            objSrvBlog = Nothing

            strReturn = ""
            Dim objSrvDev As New dev.meotom.services.MeoTomSoapClient
            strReturn = objSrvDev.UpdateHomePage("MeoTom.Net", "m2E6o8T2o2M10")
            If strReturn = "False" Then
                lg.WriteLog("False dev.meotom.net")
            ElseIf strReturn = "Fail To Login" Then
                lg.WriteLog("Fail To Login Exception Dev.meotom.net")
            End If
            objSrvDev = Nothing
        Catch ex As Exception
            lg.WriteLog(ex.Message)
        End Try
    End Sub

    Public Function MeoTomWeatherUpdate() As Boolean
        Try
            Dim VnExpressWeatherPath As String = ConfigurationManager.AppSettings("VnExpressWeatherPath").ToString()
            Dim VnExpressWeatherFile As String = ConfigurationManager.AppSettings("VnExpressWeatherFile").ToString()

            For Each str As String In Regex.Split(VnExpressWeatherFile, ";")
                WeatherXML(UploadFolder, VnExpressWeatherPath & str, str)
            Next
            Return True
        Catch ex As Exception
            lg.WriteLog(ex.Message)
            Return False
        End Try
    End Function

    Public Function WeatherXML(ByVal UploadFolder As String, ByVal strFile As String, ByVal strFileSave As String) As Boolean
        Try
            Dim fileTemp As String = (ConfigurationManager.AppSettings("WeatherFolder").ToString() & "\") + strFileSave

            'stream data
            Dim textWrite As New StreamWriter(fileTemp, False, System.Text.Encoding.UTF8)
            textWrite.Write(GetWebContent(strFile))
            textWrite.Close()

            For Each str As String In Regex.Split(UploadFolder, ";")
                UploadFile(str, strFileSave, fileTemp)
            Next
            Return True
        Catch ex As Exception
            lg.WriteLog(ex.Message)
            Return False
        End Try
    End Function

    Public Function GetWebContent(ByVal s As String) As String
        Try
            Dim strContent As String = ""

            Dim objWebRequest As HttpWebRequest = DirectCast(WebRequest.Create(s), HttpWebRequest)
            objWebRequest.Timeout = 20000
            objWebRequest.MaximumAutomaticRedirections = 4
            objWebRequest.MaximumResponseHeadersLength = 4
            objWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
            If Not (ProxyServer.Length < 1) OrElse Not (ProxyPort.Length < 1) Then
                objWebRequest.Proxy = New System.Net.WebProxy(ProxyServer, Integer.Parse(ProxyPort))
                objWebRequest.Proxy.Credentials = New NetworkCredential(ProxyUserName, ProxyPassword, ProxyUserDomain)
            End If
            objWebRequest.Credentials = CredentialCache.DefaultCredentials

            Dim objWebResponse As HttpWebResponse = DirectCast(objWebRequest.GetResponse(), HttpWebResponse)
            Dim receiceStream As Stream = objWebResponse.GetResponseStream()
            Dim readStream As New StreamReader(receiceStream, System.Text.Encoding.UTF8)
            strContent = readStream.ReadToEnd()

            objWebResponse.Close()
            readStream.Close()

            Return Regex.Replace(strContent, "\t|\r|\n", "")
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function

    Public Function UploadFile(ByVal UploadFolder As String, ByVal UploadName As String, ByVal UploadPath As String) As Boolean
        Try
            Dim ftp1 As New Utilities.FTP.FTPclient(FTPServer, UserFTP, PasswordFTP)
            ftp1.CurrentDirectory = "/" & UploadFolder
            Return ftp1.Upload(UploadPath, UploadName)
        Catch ex As Exception
            lg.WriteLog(ex.Message & "==> Upload Error !!")
            Return False
        End Try
    End Function
End Module
